package com.atguigu.gmall.product.controller;

import com.atguigu.gmall.common.result.Result;
import com.atguigu.gmall.product.entity.BaseAttrInfo;
import com.atguigu.gmall.product.entity.BaseAttrValue;
import com.atguigu.gmall.product.service.BaseAttrInfoService;
import com.atguigu.gmall.product.service.BaseAttrValueService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 平台属性
 */
@RestController
@RequestMapping("/admin/product")
public class BaseAttrController {
    @Autowired
    private BaseAttrInfoService baseAttrInfoService;
    @Autowired
    private BaseAttrValueService baseAttrValueService;

    /**
     * 添加分类id获取平台属性
     * @param category1Id
     * @param category2Id
     * @param category3Id
     * @return
     */
    @GetMapping("/attrInfoList/{category1Id}/{category2Id}/{category3Id}")
    public Result getBaseAttr(@PathVariable("category1Id") Long category1Id ,
                              @PathVariable("category2Id") Long category2Id ,
                              @PathVariable("category3Id") Long category3Id
                               ){

        List<BaseAttrInfo> baseAttrInfoList = baseAttrInfoService.getBaseAttrValue(category1Id,category2Id,category3Id);
         return  Result.ok(baseAttrInfoList);
    }

    /**
     * 添加平台属性
     * @return
     */
    @ApiOperation("保存属性的名和值/或者修改")
    @PostMapping("/saveAttrInfo")
    public  Result  saveAttrInfo(@RequestBody BaseAttrInfo baseAttrInfo){
        if(baseAttrInfo.getId()==null){
        //保存的时候     BaseAttrInfo 中 的id==null
        baseAttrInfoService.saveAttrInfo(baseAttrInfo);
        }else {
            //修改的时候  有 id值
            baseAttrInfoService.updateByAttrId(baseAttrInfo);
        }
        return Result.ok();
    }

    /**
     * 获取平台属性的id
     * @return
     */
    @GetMapping("/getAttrValueList/{attrId}")
    public  Result getByAttrId(@PathVariable ("attrId") Long attrId){
        List<BaseAttrValue> baseAttrValue= baseAttrValueService.getValueList(attrId);
        return  Result.ok(baseAttrValue);

    }


}
